package com.swotplus.admin.web.weixin;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.swotplus.common.utils.StringUtils;
import com.swotplus.system.weixin.entity.WxcmsMediaFiles;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swotplus.common.core.AjaxResult;
import com.swotplus.framework.web.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import com.swotplus.system.weixin.service.WxcmsMsgNewsService;
import java.util.*;
import com.swotplus.system.weixin.entity.WxcmsMsgNews;
import com.swotplus.system.weixin.service.WxcmsMsgNewsService;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hugang
 * @since 2020-06-04
 */
@Controller
@RequestMapping("/wxcmsMsgNews")
public class WxcmsMsgNewsController extends BaseController {

    private String prefix = "/weixin/msg/news";


    @Autowired
    private WxcmsMsgNewsService wxcmsMsgNewsService;

    @RequestMapping("/list")
    @RequiresPermissions("wxcmsMsgNews:view")
    public String toHtml(){
         return prefix + "/document";
    }

    @RequestMapping("/listData")
    @RequiresPermissions("wxcmsMsgNews:list")
    @ResponseBody
    public AjaxResult listData(WxcmsMsgNews wxcmsMsgNews, Integer page, Integer limit){
        try
        {
            QueryWrapper<WxcmsMsgNews> wrapper = new QueryWrapper<>();
            if(StringUtils.isNotBlank(wxcmsMsgNews.getMultType())){
                wrapper.eq("mult_type",wxcmsMsgNews.getMultType());
            }
            if(StringUtils.isNotBlank(wxcmsMsgNews.getTitle())){
                wrapper.eq("title",wxcmsMsgNews.getTitle());
            }
            wrapper.orderByDesc("create_time");
            Page<WxcmsMsgNews> pageList = wxcmsMsgNewsService.page(new Page<>(page,limit), wrapper);
            return AjaxResult.success("成功", (int) pageList.getTotal(),pageList.getRecords());
        } catch (Exception e)
        {
            return AjaxResult.warn(e.getMessage());
        }

    }

    /**
     * 添加多图文
     * @return
     */
    @RequestMapping("/addmultiple")
    @RequiresPermissions("wxcmsMsgNews:add")
    public String toAddmultipleHtml(){
        return prefix + "/addmultiple";
    }

    /**
     * 添加单图文
     * @return
     */
    @RequestMapping("/addsingle")
    @RequiresPermissions("wxcmsMsgNews:add")
    public String toAddsingleHtml(){
        return prefix + "/addsingle";
    }

    /**
     * 新增保存
     */
    @RequestMapping("/add")
    @RequiresPermissions("wxcmsMsgNews:add")
    @ResponseBody
    public AjaxResult add(WxcmsMsgNews wxcmsMsgNews){

        try
        {
            return toAjax(wxcmsMsgNewsService.save(wxcmsMsgNews));
        } catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
    * 删除
    */
    @RequestMapping("/remove")
    @RequiresPermissions("wxcmsMsgNews:del")
    @ResponseBody
    public  AjaxResult delBatch(String ids){

        try
        {
            List<String> ListStr = Arrays.asList(ids.split(",")).stream().map(s -> s.trim()).collect(Collectors.toList());
            return toAjax(wxcmsMsgNewsService.removeByIds(ListStr));
        } catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
    * 编辑
    */
    @RequestMapping("/edit")
    @RequiresPermissions("wxcmsMsgNews:edit")
    @ResponseBody
    public AjaxResult edit(WxcmsMsgNews wxcmsMsgNews){
       try
        {
            return toAjax(wxcmsMsgNewsService.updateById(wxcmsMsgNews));
        } catch (Exception e)
        {
            return error(e.getMessage());
        }


    }

}

